<?php
/**
 * @author Jumper Swordwave
 * date: 2021/04/21
 */
declare(strict_types=1);
namespace App\Game\Service;

use Psr\Container\ContainerInterface;
use Hyperf\Redis\Redis;
use App\Log;

/**
 * 数据管理基类
 */
class BaseService
{
    /**
     * DI容器
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var Redis
     */
    protected $redis;

    /**
     * @var Logger
     */
    protected $log;
    
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        $this->redis = $container->get(Redis::class);
        $this->log = Log::get();
    }
    
    protected function get(string $key)
    {
        $obj = $this->redis->get($key);
        if ($obj) {
            return unserialize($obj);
        }
        return null;
    }
    
    protected function save(string $key, $obj)
    {
        $this->redis->set($key, serialize($obj));
    }
    
    protected function del(string $key) : void
    {
        $this->redis->del($key);
    }
    
    protected function expire(string $key, int $seconds) : void
    {
        $this->redis->expire($key, $seconds);
    }
}